package com.go.ssh.point.web.front.goods.action;

import com.go.ssh.commons.pagination.Page;
import com.go.ssh.commons.web.action.BaseAction;
import com.go.ssh.point.exception.NotCodeException;
import com.go.ssh.point.model.GoodsCodeModel;
import com.go.ssh.point.model.GoodsModel;
import com.go.ssh.point.service.IGoodsCodeService;
import com.go.ssh.point.service.IGoodsService;

@SuppressWarnings("serial")
public class GoodsAction extends BaseAction {
	private static final String BUY_RESULT = "buyResult";
	IGoodsService goodsService;
	IGoodsCodeService goodsCodeService;
	private int goodsId;
	private Page<GoodsModel> page;

	public String list() {
		// getValueStack().set(PAGE, goodsService.listAllPublished(getPn()));
		page = goodsService.listAllPublished(getPn());
		return LIST;
	}

	public String buy() {
		String username = "test";
		GoodsCodeModel goodsCode = null;
		try {
			goodsCode = goodsCodeService.buy(username, goodsId);
		} catch (NotCodeException e) {
			this.addActionError("没有足够的兑换码了");
			return BUY_RESULT;
		} catch (Exception e) {
			e.printStackTrace();
			this.addActionError("未知错误");
			return BUY_RESULT;
		}
		this.addActionMessage("购买成功，您的兑换码为 :" + goodsCode.getCode());

		// TODO 给单元测试带来麻烦
		// TODO 应该用属性？或 Model？
		// getValueStack().set(MODEL, goodsCode);
		return BUY_RESULT;
	}

	public void setGoodsId(int goodsId) {
		this.goodsId = goodsId;
	}

	public void setGoodsService(IGoodsService goodsService) {
		this.goodsService = goodsService;
	}

	public void setGoodsCodeService(IGoodsCodeService goodsCodeService) {
		this.goodsCodeService = goodsCodeService;
	}

	public Page<GoodsModel> getPage() {
		return page;
	}
}
